//保存用户信息
function saveInfo(){
	$.ajax({
		url : "/dancoder/user/saveinfo",
		type : 'POST',
		dataType : 'json',
		contentType : 'text/html',
		data : JSON.stringify({
			nickName : $("nike-name").val(),
			userSex : $("user-sex").val(),
			userEmail : $("user-email").val(),
			userDesc : $("user-desc").val(),
			userWebsite : $("website").val(),
			emailValidate : $("email-validate").val()
		}),
		success : function(data, status) {
			var status = status;
			if (status.toString() == "success") {
				if (data.message && data.message.length > 0) {
					alert(data.message);
					window.location.href = "../index.html";
				} else if (data.error && data.error.length > 0) {
					alert(data.error);
				}
			} else {
				window.location.href = "";
			}
		},
		error : function() {
			alert(data.error);
		}
	});
	return true;
};

// 发送邮件 
function sendEmail() {
	var url = "/dancoder/user/sendemail";
	var uppw_username = $("#uppw-username").val()
	var uppw_newpw = $("#uppw-newpw").val()
	var uppw_confirmpassword = $("#uppw-confirmpassword").val()
	var uppw_email = $("#uppw-email").val();

	if (uppw_username == "") {
		alert("用户名不能为空");
		return false;
	} else if (uppw_newpw == "") {
		alert("密码不能为空");
		return false;
	} else if (uppw_confirmpassword == "") {
		alert("重复密码不能为空");
		return false;
	} else if (uppw_newpw != uppw_confirmpassword) {
		alert("两次密码不一致");
		return false;
	} else if (uppw_email == "") {
		alert("邮箱不能为空");
		return false;
	} else {
		$.ajax({
			url : url,
			type : 'POST',
			dataType : 'json',
			contentType : 'text/html',
			data : JSON.stringify({
				userName : $("#uppw-username").val(),
				userPassword : $("#uppw-newpw").val(),
				userEmail : $("#uppw-email").val()
			}),
			success : function(data, status) {
				var status = status;
				if (status.toString() == "success") {
					alert(data.message);
				} else {
					window.location.href = "";
				}
			},
			error : function() {
				alert(data.error);
			}
		});
		return true;
	}
};

// 修改密码
function updatepwd() {
	var url = "/dancoder/user/updatepwd";
	var uppw_username = $("#uppw-username").val()
	var uppw_newpw = $("#uppw-newpw").val()
	var code = $("#code").val()
	$.ajax({
		url : url,
		type : 'POST',
		dataType : 'json',
		contentType : 'text/html',
		data : JSON.stringify({
			userName : $("#uppw-username").val(),
			userPassword : $("#uppw-newpw").val(),
			randomText : $("#code").val()
		}),
		success : function(data, status) {
			var status = status;
			if (status.toString() == "success") {
				if (data.message && data.message.length > 0) {
					alert(data.message);
					window.location.href = "../index.html";
				} else if (data.error && data.error.length > 0) {
					alert(data.error);
				}
			} else {
				window.location.href = "";
			}
		},
		error : function() {
			alert(data.error);
		}
	});
	return true;
}

// 登录
function login() {
	var url = "/dancoder/user/login"
	var name = $("#user-name").val()
	var psw = $("#user-psw").val();
	if ($("#user-name").val() == "") {
		alert("用户名不能为空");
		return false;
	} else if ($("#user-psw").val() == "") {
		alert("密码不能为空");
		return false;
	} else {
		$.ajax({
			url : url,
			type : 'POST',
			dataType : 'json',
			contentType : 'text/html',
			data : JSON.stringify({
				userName : $("#user-name").val(),
				userPassword : $("#user-psw").val()
			}),
			success : function(data, status) {
				var status = status;
				if (status.toString() == "success") {
					if (data.message && data.message.length > 0) {
						alert(data.message);
						top.window.location = "../index.html";
					} else if (data.error && data.error.length > 0) {
						alert(data.error);
					}
				} else {
					window.location.href = "login.html";
				}
			},
			error : function() {
				alert("用户名密码验证失败")
			}
		});
		return true;
	}
};

// 注册
function register() {
	var imgSession = getImgCode()
	var url = "/dancoder/user/register"
	var reUserName = $("#username").val()
	var reUserPw = $("#password").val()
	var reConfirmpassword = $("#confirmpassword").val()
	var number = $("#code").val();
	if (reUserName == "") {
		alert("用户名不能为空");
		return false;
	} else if (reUserPw == "") {
		alert("密码不能为空");
		return false;
	} else if (reConfirmpassword == "") {
		alert("密码不能为空");
		return false;
	} else if (reUserPw != reConfirmpassword) {
		alert("两次密码不一致");
		return false;
	} else if (number == "") {
		alert("请输入验证码");
		return false;
	} else if (number != imgSession) {
		alert("验证码错误，请重新输入");
		return false;
	} else {
		$.ajax({
			url : url,
			async : true,
			type : 'POST',
			dataType : 'json',
			contentType : 'text/html',
			data : JSON.stringify({
				userName : reUserName,
				userPassword : reUserPw
			}),
			success : function(data, status) {
				var status = status;
				if (status.toString() == "success") {
					if (data.message && data.message.length > 0) {
						alert(data.message);
						window.location.href = "../user/login.html";
					} else if (data.error && data.error.length > 0) {
						alert(data.error);
					}
				} else {
					window.location.href = "";
				}
			},
			error : function() {
				alert("用户名密码验证失败")
			}
		});
		return true;
	}
};

// 获取session中的img验证码
function getImgCode() {
	var imgSession = ""
	var url = "/dancoder/user/getimgsession";
	$.ajax({
		async : false,
		url : url,
		type : 'GET',
		dataType : 'json',
		success : function(data, status) {
			var status = status
			if (status.toString() == "success") {
				imgSession = data.imgCode
			}
		},
		error : function() {
			alert("获取imgCode失败")
		}
	});
	return imgSession;
};

// 重写alert
window.alert = function(message) {
	$.globalMessenger().post({
		message : message,
		hideAfter : 3,
		showCloseButton : true
	});
};
/*
 * function getUserName(){ var userName ="" var url =
 * "/dancoder/user/getsession"; $.ajax({ async: false, url: url, type: 'GET',
 * dataType : 'json', success: function (data, status) { var status = status if
 * (status.toString() == "success") { userName=data.userName } }, error:
 * function () { alert("获取name失败") } }); return userName; }
 */

